# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: item_demo.py
@time: 2021/01/29
"""

class Person:
    def __init__(self,name):
        self.name = name

    def __getitem__(self, item):
        print(item)
        return self.name[item]


def test01():
    ...
    per = Person('tony')
    # for n in per:
    #     print(n)
    # ns = list(per)
    # print(ns)
    # for循环拿出每一个与y对比，相等就返回True,循环结束没有相等的，返回False
    print('a' in per)

class A:
    # def __bool__(self):
    #     print('boo')
    #     return False

    def __len__(self):
        print('11')
        return 1

    def __getattr__(self, item):
        return 'A'

    # def __getattribute__(self, item):
    #     # print('111')
    #     return "B"

    def __setattr__(self, key, value):
        if key == 'age':
            self.__dict__[key] = value
        else:
            raise Exception('not allow attr')

    def __repr__(self):
        return 'a' * 10

    def __add__(self, other):
        print(other)
        return 1 + other

    def __radd__(self, other):
        print(other)
        return 2 + other

def test02():
    ...
    a = A()
    if bool(a):
        print('111')

    print(a)
    a.age = 10
    print(a.age)
    print(1 + a)
    print(a + 1)

if __name__ == '__main__':
    pass
    test02()

